import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {ActivatedRoute} from '@angular/router';
import {iterator} from 'rxjs/internal-compatibility';

@Component({
  selector: 'app-report-info',
  templateUrl: './report-info.component.html',
  styleUrls: ['./report-info.component.scss']
})
export class ReportInfoComponent {


  color = ['#5470c6', '#91cc75', '#fac858', '#ee6666',
    '#73c0de', '#3ba272', '#fc8452', '#9a60b4',
    '#ea7ccc'];
  deviceInterfaceList = [
    {title: 'DVI接口测试', key: '1001', isPass: true},
    {title: 'VGA接口测试', key: '1002', isPass: true},
    {title: '视频接口切换测试', key: '1003', isPass: true},
    {title: 'PAL接口测试', key: '1004', isPass: true},
    {title: 'RS422接口测试', key: 'RS422', isPass: true},
    {title: 'RS232接口测试', key: 'RS232', isPass: true},
    {title: '音频输入测试', key: '1006', isPass: true},
    {title: '音频输出测试', key: '1007', isPass: true},
    {title: 'USB传输测试', key: '1008', isPass: true},
    {title: 'PS/2接口传输测试', key: '1009', isPass: true},
    {title: 'IP配置', key: 'ipConfig', isPass: true},
    {title: '网口测试', key: '1011', isPass: true},
    {title: '串口测试', key: '1012', isPass: true}
  ];
  deviceStatusList: any = [
    {title: '键盘适配性测试', key: '1012', isPass: true},
    {title: '鼠标适配性测试', key: '1012', isPass: true},
    {title: '显示器适配性测试', key: '1012', isPass: true},
    {title: 'XXX适配性测试', key: '1012', isPass: true},

  ];
  // cpu平均温度
  cpuTemperatureOptions = {};
  // gpu平均温度
  gpuTemperatureOptions = {};
  // 风扇平均转速
  fanSpeedOptions = {};
  cpuOptions = {};
  gpuOptions = {};
  diskOptions = {};
  option = {
    title: {
      text: 'CPU性能测试结果',
      subtext: '基准数据来自 www.cpubenchmark.net'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      data: ['2011年']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      boundaryGap: [0, 0.01]
    },
    yAxis: {
      type: 'category',
      data: [
        'Intel Core i9-9980XE @ 3.00GHz',
        '印尼',
        '美国',
        '印度',
        '中国',
        '世界人口(万)']
    },
    series: [
      {
        name: '2011年',
        type: 'bar',
        data: [32402, 23489, 29034, 104970, 131744, 630230]
      }
    ]
  };

  cpuBenchMark = [
    ['Intel Core i9-9980XE @ 3.00GHz', 32402],
    ['Intel Core i9-11900K @ 3.50GHz', 25802],
    ['Intel Core i7-11700F @ 2.50GHz', 21024],

    ['Intel Core i7-10700 @ 2.90GHz', 17360],
    ['Intel Core i7-9700K @ 3.60GHz', 14546],
    ['Intel Core i5-11300H @ 3.10GHz', 11095],

    ['Intel Core i7-8750H @ 2.20GHz', 10172],
    ['Intel Core i5-8300H @ 2.30GHz', 7544],
    ['Intel Core i5-6440HQ @ 2.60GHz', 5151],
    ['Intel Core i5-2540M @ 2.60GHz', 2302]

  ];
  gpuBenchMark = [
    ['Radeon RX 6900 XT', 26058],
    ['GeForce RTX 3060 Ti', 19712],
    ['Radeon RX 5700', 14651],

    ['GeForce GTX 1060', 9922],
    ['GeForce GTX 780', 8008],
    ['Radeon R9 285', 6680],

    ['Radeon Pro 465', 4537],
    ['GeForce GTX 770M', 2840],
    ['GeForce GTX 650', 1760]
  ];
  report: any;

  constructor(route: ActivatedRoute, public http: HttpClient) {
    route.params.subscribe(data => {
      this.http.post('getOne', {entityName: 'DmzReport', PK: {reportId: data.reportId}}).subscribe((value) => {
        this.report = value;
        this.report.json = JSON.parse(this.report.json);
        console.log('report data: ', JSON.stringify(this.report));
        this.init();
      });
    });
  }

  init(): void {

    this.cpuTemperatureOptions = this.makeGaugeOptions('CPU温度', 100, 5, '{value}°C', this.report.json.temperatureAvgData.cpuTemp.avg);
    this.gpuTemperatureOptions = this.makeGaugeOptions('GPU温度', 100, 5, '{value}°C', this.report.json.temperatureAvgData.gpuTemp.avg);
    this.fanSpeedOptions = this.makeGaugeOptions('风扇转速', 6000, 6, '{value}rpm', 2365);

  }

  makeGaugeOptions(name, max, splitNumber, detailFormatter, value): any {
    return {
      tooltip: {formatter: '{b} : {c}%'},
      series: [{
        name, type: 'gauge', max, splitNumber,
        axisLine: {
          lineStyle: {
            // width: 30,
            color: [
              [0.3, '#67e0e3'],
              [0.7, '#37a2da'],
              [1, '#fd666d']
            ]
          }
        },
        pointer: {
          itemStyle: {
            color: 'auto'
          }
        },
        // progress: {show: true},
        title: {fontSize: 14},
        detail: {
          width: 40,
          height: 14,
          fontSize: 14,
          color: '#fff',
          backgroundColor: 'auto',
          borderRadius: 3,
          formatter: detailFormatter
        },
        data: [{
          value, name,
          title: {offsetCenter: ['0%', '80%']},
          detail: {width: 80, offsetCenter: ['0%', '99%']}
        }]
      }]
    };
  }

  makeBarOptions(source): any {
    const series = [];
    const length = Object.keys(source[0]).length - 1;
    for (let i = 0; i < length; i++) {
      series.push({type: 'bar'});
    }

    return {
      legend: {},
      tooltip: {},
      dataset: {source},
      xAxis: {type: 'category'},
      yAxis: {},
      series
    };
  }

  getPerformanceOption(testItem): any {
    let result = testItem.result;
    if (!result) {
      result = {};
    }
    if (testItem.key === 'cpuTest') {
      return this.getBenchMarkOption('CPU性能测试结果', '参考数据来自 www.cpubenchmark.net', {
        label: this.report.json.baseInfo.cpu[0].name,
        value: result.kwips
      }, this.cpuBenchMark);
    } else if (testItem.key === 'gpuTest') {
      return this.getBenchMarkOption('GPU性能测试结果', '参考数据来自 www.videocardbenchmark.net', {
        label: this.report.json.baseInfo.gpu[0].caption,
        value: parseFloat(result.frameRate)
      }, this.gpuBenchMark);
    } else if (testItem.key === 'diskTest') {
      return this.makeRankOption('磁盘测试结果', '展示磁盘读写速率', ['读取速率(MB/s)', '写入速率(MB/s)'],
        [result.readScore, result.writeScore]);
    }
  }

  getBenchMarkOption(text, subtext, myItem: { label: string, value: number }, benchMarkArr): any {
    const xData = [];
    const sData = [];
    let inserted = false;
    for (const item of benchMarkArr) {
      const itemLabel = item[0];
      const itemValue = item[1];
      if (!inserted && itemValue < myItem.value) {
        xData.push({
          value: '被测设备：' + myItem.label,
          textStyle: {
            color: '#a90000'
          }
        });

        sData.push({
          value: myItem.value,
          itemStyle: {
            color: '#a90000'
          }
        });
        inserted = true;
      }
      xData.push(itemLabel);
      sData.push(itemValue);
    }
    return this.makeRankOption(text, subtext, xData.reverse(), sData.reverse());
  }

  makeRankOption(text, subtext, xData, sData): any {
    return {
      title: {
        text,
        subtext
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      legend: {
        show: false,
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'value',
        boundaryGap: [0, 0.01]
      },
      yAxis: {
        type: 'category',
        data: xData
      },
      series: [
        {
          // name: '',
          type: 'bar',
          data: sData
        }
      ]
    };
  }

  get memoryInfo(): Array<string> {
    const memory = this.report.json.baseInfo.memory as Array<any>;
    return memory.map(item => {
      const capacity = Math.ceil(item.capacity / (1024 * 1024 * 1024));
      return capacity + 'GB' + '(' + item.manufacturer + ')';
    });
  }

  get diskInfo(): Array<string> {
    const disk = this.report.json.baseInfo.disk as Array<any>;
    return disk.map(item => {
      const size = Math.ceil(item.size / (1024 * 1024 * 1024));
      return item.name + '(' + size + 'GB)';
    });
  }

  get gpuInfo(): Array<string> {
    const arr = this.report.json.baseInfo.gpu as Array<any>;
    return arr.map(item => {
      const size = Math.ceil(item.rawSize / (1024 * 1024 * 1024));
      return item.caption + '(' + size + 'GB)';
    });
  }

  get interfaceTestList(): Array<any> {
    return (this.report.json.testItems as Array<any>).filter(item => {
      return item.testType === 'interfaceTest';
    });
  }

  get statusTestList(): Array<any> {
    return (this.report.json.testItems as Array<any>).filter(item => {
      return item.testType === 'statusTest';
    });
  }

  get performanceTestList(): Array<any> {
    return (this.report.json.testItems as Array<any>).filter(item => {
      return item.testType === 'performanceTest';
    });
  }

  print(): void {
    window.print();
  }
}
